﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace SmartPaginator.Elements
{
    public static class BlockHelper
    {
        public static Thickness GetAdjustedMargin(IBlockElement elementA, IBlockElement elementB, Thickness sourceMargin)
        {
            double topMargin = 0;

            if (elementA != null)
            {
                // Get the bottom margin of the elementA
                topMargin = elementA.CalculateBottomMargin();

                // Apply element's B top margin instead if it is bigger
                var top = elementB.CalculateTopMargin();
                if (top > topMargin)
                    topMargin = top;
            }
            else
            {
                // No previous blocks. We should only apply the margin if it is not auto
                if (!double.IsNaN(sourceMargin.Top))
                    topMargin = sourceMargin.Top;
            }

            double left = 0;
            double right = 0;
            double bottom = 0;
            if (!double.IsNaN(sourceMargin.Left) && !double.IsInfinity(sourceMargin.Left))
                left = sourceMargin.Left;
            if (!double.IsNaN(sourceMargin.Right) && !double.IsInfinity(sourceMargin.Right))
                right = sourceMargin.Right;
            if (!double.IsNaN(sourceMargin.Bottom) && !double.IsInfinity(sourceMargin.Bottom))
                bottom = sourceMargin.Bottom;

            return new Thickness(left, topMargin, right, bottom);
        }

    }
}
